@using ZR.CodeGenerator
@using ZR.Model.System.Generate
@using Infrastructure.Extensions;
@* @model ZR.CodeGenerator.Model.GenerateDto; *@
@{
    var genOptions = Model.GenOptions;
    var genTable = Model.GenTable;
    var genTableOptions=genTable.Options;
    var funCodeFirstLower=genTable.FunctionCode.FirstLowerCase();
    
    var replaceDto = Model.ReplaceDto;
    bool showCustomInput = Model.GenTable.Columns.Any((Func<GenTableColumn, bool>)(f => f.HtmlType.Equals(GenConstants.HTML_CUSTOM_INPUT, StringComparison.OrdinalIgnoreCase)));
}
@* //创建时间：@(replaceDto.AddTime) *@
using Microsoft.AspNetCore.Mvc;
using Infrastructure.Filters;
using ZR.Infrastructure;
using @(genOptions.IServicsNamespace).@(genOptions.SubNamespace).IService;
using @(genOptions.DtosNamespace).@(genOptions.SubNamespace).Dto;
using @(genOptions.DtosNamespace).@(genOptions.SubNamespace).Entity;
using @(genOptions.ModelsNamespace);
using @(genOptions.ApiControllerNamespace).MainModules;
using ZR.ServiceCore.Attributes;
@if (replaceDto.ShowBtnImport)
{
    @:using MiniExcelLibs;
}

namespace @(genOptions.ApiControllerNamespace).Controllers.@(genOptions.SubNamespace)
{
    /// <summary>
    /// @(genTable.FunctionName)
    /// </summary>
    public class @(genTable.FunctionCode)ControllerBase : ApiControllerBase
    {

        /// <summary>
        /// @(genTable.FunctionName)接口
        /// </summary>
        protected readonly I@(genTable.FunctionCode)Service _@(genTable.FunctionCode)Service;

        protected readonly MapsterAdaptEntityCfg _mapsterAdaptEntityCfg;

        public @(genTable.FunctionCode)ControllerBase(I@(genTable.FunctionCode)Service @(funCodeFirstLower)Service, MapsterAdaptEntityCfg mapsterAdaptEntityCfg)
        {
            _@(genTable.FunctionCode)Service = @(funCodeFirstLower)Service;
            _mapsterAdaptEntityCfg = mapsterAdaptEntityCfg;
        }

@if (replaceDto.ShowBtnAdd)
{
        @:/// <summary>
        @:/// 添加@(genTable.FunctionName)
        @:/// </summary>
        @:/// <returns></returns>
        @:[HttpPost("add")]
        @:[ActionPermissionFilter(Permission = "@(replaceDto.PermissionPrefix):add")]
        @:[Log(Title = "@(genTable.FunctionName)", BusinessType = BusinessType.INSERT)]
        @:public virtual async Task Add@(genTable.FunctionCode)([FromBody] @(genTable.FunctionCode)FormDto form)
        @:{
            @:await CheckFormParam(form);
            @:var model = form.Adapt<@(genTable.EntityClassName)>(_mapsterAdaptEntityCfg).ToCreate(HttpContext, EntityExtension.AutoFillField2);
            @:var isOk = await _@(genTable.FunctionCode)Service.Add@(genTable.FunctionCode)(model);
            @:if (!isOk)
            @:{
                @:throw new CustomException(ResultCode.FAIL, "添加失败");
            @:}
        @:}
}

@if (replaceDto.ShowBtnEdit)
{
        @:/// <summary>
        @:/// 修改@(genTable.FunctionName)
        @:/// </summary>
        @:/// <returns></returns>
        @:[HttpPost("modify")]
        @:[ActionPermissionFilter(Permission = "@(replaceDto.PermissionPrefix):edit")]
        @:[Log(Title = "@(genTable.FunctionName)", BusinessType = BusinessType.UPDATE)]
        @:public virtual async Task Modify@(genTable.FunctionCode)([FromBody] @(genTable.FunctionCode)FormDto form)
        @:{
            @:await CheckFormParam(form);
            @:var model = form.Adapt<@(genTable.EntityClassName)>(_mapsterAdaptEntityCfg).ToUpdate(HttpContext);
            @:var isOk = await _@(genTable.FunctionCode)Service.Modify@(genTable.FunctionCode)(model);
            @:if (!isOk)
            @:{
                @:throw new CustomException(ResultCode.FAIL, "更新失败");
            @:}
        @:}
}

@if (replaceDto.ShowBtnDelete || replaceDto.ShowBtnMultiDel)
{
        @:/// <summary>
        @:/// 删除@(genTable.FunctionName)
        @:/// </summary>
        @:/// <returns></returns>
        @:[HttpPost("delete")]
        @:[ActionPermissionFilter(Permission = "@(replaceDto.PermissionPrefix):delete")]
        @:[Log(Title = "@(genTable.FunctionName)", BusinessType = BusinessType.DELETE)]
        @:public virtual async Task Delete@(genTable.FunctionCode)([FromBody] Delete@(genTable.FunctionCode)Req req)
        @:{
            @:var idArr = Tools.SplitAndConvert<long>(req.Ids);
            @:var isOk = await _@(genTable.FunctionCode)Service.Delete@(genTable.FunctionCode)(idArr, false);
            @:if (!isOk)
            @:{
                @:throw new CustomException(ResultCode.FAIL, "删除失败");
            @:}
        @:}
}

@if (genTableOptions.IsEnableStateChange)
{
        @:/// <summary>
        @:/// 变更@(genTable.FunctionName)状态
        @:/// </summary>
        @:/// <returns></returns>
        @:[HttpPost("changeState")]
        @:[ActionPermissionFilter(Permission = "@(replaceDto.PermissionPrefix):changestate")]
        @:[Log(Title = "@(genTable.FunctionName)", BusinessType = BusinessType.UPDATE)]
        @:public virtual async Task Change@(genTable.FunctionCode)State([FromBody] Change@(genTable.FunctionCode)StateReq req)
        @:{
            @:var isOk = await _@(genTable.FunctionCode)Service.Change@(genTable.FunctionCode)State(req);
            @:if (!isOk)
            @:{
                @:throw new CustomException(ResultCode.FAIL, "更新失败");
            @:}
        @:}
}


@if (replaceDto.ShowBtnExport)
{
        @:/// <summary>
        @:/// 导出@(genTable.FunctionName)列表
        @:/// </summary>
        @:/// <returns></returns>
        @:[Log(Title = "@(genTable.FunctionName)", BusinessType = BusinessType.EXPORT, IsSaveResponseData = false)]
        @:[HttpGet("exportList")]
        @:[ActionPermissionFilter(Permission = "@(replaceDto.PermissionPrefix):exportList")]
        @:[IgnoreRspContract]
        @:public virtual async Task<IActionResult> Export@(genTable.FunctionCode)List([FromQuery] Get@(genTable.FunctionCode)PageListReq req)
        @:{
            @:req.PageNum = 1;
            @:req.PageSize = 100000;
            @:var listInfo = await _@(genTable.FunctionCode)Service.Get@(genTable.FunctionCode)PageList(req);
            @:if (listInfo.Result == null || listInfo.Result.Count <= 0)
            @:{
                @:throw new CustomException(ResultCode.NO_DATA,"没有要导出的数据");
            @:}
            @:var result = await ExportExcelMiniAsync(listInfo.Result, "@(genTable.FunctionName)", "@(genTable.FunctionName)");
            @:return ExportExcel(result.Item2, result.Item1);
        @:}
}

@if(replaceDto.ShowBtnImport)
{
        @:/// <summary>
        @:/// 导入
        @:/// </summary>
        @:/// <param name="formFile"></param>
        @:/// <returns></returns>
        @:[HttpPost("importData")]
        @:[Log(Title = "@(genTable.FunctionName)导入", BusinessType = BusinessType.IMPORT, IsSaveRequestData = false)]
        @:[ActionPermissionFilter(Permission = "@(replaceDto.PermissionPrefix):import")]
        @:public IActionResult ImportData([FromForm(Name = "file")] IFormFile formFile)
        @:{
            @:List<@(genTable.FunctionCode)Dto> list = new();
            @:using (var stream = formFile.OpenReadStream())
            @:{
                @:list = stream.Query<@(genTable.FunctionCode)Dto>(startCell: "A1").ToList();
            @:}

            @:return SUCCESS(_@(genTable.FunctionCode)Service.Import@(genTable.FunctionCode)(list.Adapt<List<@(genTable.FunctionCode)>>()));
        @:}

        @:/// <summary>
        @:/// @(genTable.FunctionName)导入模板下载
        @:/// </summary>
        @:/// <returns></returns>
        @:[HttpGet("importTemplate")]
        @:[Log(Title = "@(genTable.FunctionName)模板", BusinessType = BusinessType.EXPORT, IsSaveResponseData = false)]
        @:[AllowAnonymous]
        @:public IActionResult ImportTemplateExcel()
        @:{
            @:var result = DownloadImportTemplate(new List<@(genTable.FunctionCode)Dto>() { }, "@(genTable.FunctionCode)");
            @:return ExportExcel(result.Item2, result.Item1);
        @:}

}
@if(showCustomInput)
{
        @:/// <summary>
        @:/// 保存排序
        @:/// </summary>
        @:/// <param name="id">主键</param>
        @:/// <param name="value">排序值</param>
        @:/// <returns></returns>
        @:[ActionPermissionFilter(Permission = "@(replaceDto.PermissionPrefix):edit")]
        @:[HttpGet("ChangeSort")]
        @:[Log(Title = "保存排序", BusinessType = BusinessType.UPDATE)]
        @:public IActionResult ChangeSort(@(genTable.PkColumnType) id = 0, int value = 0)
        @:{
            @:if (id <= 0) { return ToResponse(ApiResult.Error(101, "请求参数错误")); }
            @:var response = _@(genTable.FunctionCode)Service.Update(w => w.@(replaceDto.PKName) == id, it => new @(genTable.FunctionCode)()
            @:{
        @foreach (var item in genTable.Columns)
        {
            if (item.HtmlType == "customInput")
            {
                @:@item.CsharpField = value,
            }
        }
            @:});
            @:
            @:return ToResponse(response);
        @:}
}

@if(genTableOptions.IsTreeTable)
{
        @:/// <summary>
        @:/// 查询@(genTable.FunctionName)树选择列表
        @:/// </summary>
        @:/// <param name="req"></param>
        @:/// <returns></returns>
        @:[HttpPost("treeSelectList")]
        @:[ActionPermissionFilter(Permission = "@(replaceDto.PermissionPrefix):treeSelectList")]
        @:public virtual async Task<List<TreeNodeInfo>> Get@(genTable.FunctionCode)TreeSelectList([FromBody] Get@(genTable.FunctionCode)TreeSelectListReq req)
        @:{
            @:var result = await _@(genTable.FunctionCode)Service.Get@(genTable.FunctionCode)TreeSelectList(req);
            @:return result;
        @:}
}

@if (genTable.TplCategory == "tree")
{
        @:/// <summary>
        @:/// 查询@(genTable.FunctionName)列表树
        @:/// </summary>
        @:/// <param name="parm"></param>
        @:/// <returns></returns>
        @:[HttpGet("treeList")]
        @:[ActionPermissionFilter(Permission = "@(replaceDto.PermissionPrefix):list")]
        @:public IActionResult QueryTree@(genTable.FunctionCode)([FromQuery] @(genTable.FunctionCode)QueryDto parm)
        @:{
            @:var response = _@(genTable.FunctionCode)Service.GetTreeList(parm);
            @:return SUCCESS(response);
        @:}
}

        /// <summary>
        /// 查询@(genTable.FunctionName)分页列表
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost("pageList")]
        [ActionPermissionFilter(Permission = "@(replaceDto.PermissionPrefix):list")]
        public virtual async Task<PagedInfo<@(genTable.FunctionCode)ListItemDto>> Get@(genTable.FunctionCode)PageList([FromBody] Get@(genTable.FunctionCode)PageListReq req)
        {
            var result = await _@(genTable.FunctionCode)Service.Get@(genTable.FunctionCode)PageList(req);
            return result;
        }

        /// <summary>
        /// 查询@(genTable.FunctionName)详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("detail/{id}")]
        [ActionPermissionFilter(Permission = "@(replaceDto.PermissionPrefix):detail")]
        public virtual async Task<@(genTable.FunctionCode)DetailDto> Get@(genTable.FunctionCode)Detail(long id)
        {
            var result = await _@(genTable.FunctionCode)Service.Get@(genTable.FunctionCode)Detail(id);
            return result;
        }

    }
}